package xyz.xiezc.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xyz.xiezc.www.entity.MzituBean;
import xyz.xiezc.www.entity.MzituImgBean;
import xyz.xiezc.www.mapper.MzituBeanMapper;
import xyz.xiezc.www.mapper.MzituImgBeanMapper;
import xyz.xiezc.www.util.XUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wb-xzc291800
 * @date 2019/04/04 10:50
 */
@Slf4j
@RequestMapping("/")
@Controller
public class IndexController {
    @Autowired
    MzituBeanMapper mzituBeanMapper;
    @Autowired
    MzituImgBeanMapper mzituImgBeanMapper;


    @GetMapping("/index_{pageNo}.html")
    public String index(Model model,
                        @PathVariable(name = "pageNo") Integer pageNo) {
        Page page = new Page();
        page.setSize(6);
        page.setCurrent(pageNo);
        LambdaQueryWrapper<MzituBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(MzituBean::getFirstImg);
        IPage mzituBeanPage = mzituBeanMapper.selectPage(page, queryWrapper);
        model.addAttribute("mzituBeanPage", mzituBeanPage);
        List<Integer> list = XUtil.createPage(mzituBeanPage);
        model.addAttribute("pageInfo", list);
        model.addAttribute("breadcrumb", "mzi");
        return "index";
    }

    @GetMapping("/info_{uniqueStr}_{index}.html")
    public String info(Model model, HttpSession httpSession,
                       @PathVariable(name = "uniqueStr") String uniqueStr,
                       @PathVariable(name = "index") Integer index,
                       @RequestParam(name = "breadcrumb") String breadcrumb
    ) {
        List<MzituImgBean> attribute = (List<MzituImgBean>) httpSession.getAttribute(uniqueStr);
        if (attribute == null) {
            LambdaQueryWrapper<MzituImgBean> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MzituImgBean::getUniqueStr, uniqueStr);
            List<MzituImgBean> mzituImgBeans = mzituImgBeanMapper.selectList(queryWrapper);
            httpSession.setAttribute(uniqueStr, mzituImgBeans);
            attribute = mzituImgBeans;
        }
        MzituImgBean mzituImgBean = attribute.get(index);
        model.addAttribute("mzituImgBean", mzituImgBean);
        model.addAttribute("breadcrumb", breadcrumb);
        return "info";
    }


}
